﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DTD2013.Common;
using System.Drawing;
using DevExpress.XtraGrid.Views.Grid.Drawing;

namespace DTD2013.DTDComponent
{
    public partial class DTDGridControl : GridControl
    {
        public DTDGridControl()
        {
            InitializeComponent();
            this.MainView = this.MainView as GridView;
        }

        public DTDGridControl(IContainer container) : this()
        {
            container.Add(this);
        }

        public virtual void InitializeControl()
        { 
            // Initialize grid control view
            InitializeGridView();
        }
        
        protected virtual GridView InitializeGridView()
        {
            GridView view = this.ViewCollection[0] as GridView;
            view.Name = string.Format("dgv{0}", this.Name.Substring(3));

            view.OptionsBehavior.AutoPopulateColumns = false;
            view.OptionsBehavior.AutoSelectAllInEditor = true;
            view.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseUp;

            view.OptionsNavigation.AutoFocusNewRow = true;

            view.OptionsView.ColumnAutoWidth = false;
            //view.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            view.OptionsView.ShowGroupPanel = false;

            view.OptionsDetail.EnableMasterViewMode = false;

            view.Appearance.HeaderPanel.Font = new Font(AppearanceRow.HeaderFontName, AppearanceRow.HeaderFontHeight);
            view.Appearance.HeaderPanel.Options.UseFont = true;
            view.Appearance.Row.Font = new Font(AppearanceRow.RowFontName, AppearanceRow.RowFontHeight);
            view.Appearance.Row.Options.UseFont = true;
            view.Appearance.OddRow.BackColor = Color.WhiteSmoke;
            view.Appearance.OddRow.Options.UseBackColor = true;

            view.RowHeight = AppearanceRow.RowHeight;
            view.ColumnPanelRowHeight = AppearanceRow.ColumnHeight;

            view.IndicatorWidth = AppearanceRow.RowIndicator;

            view.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(view_CustomDrawRowIndicator);
            
            return view;
        }

        protected virtual void view_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Appearance.Font = new Font(e.Appearance.Font.FontFamily, AppearanceRow.RowIndicatorText);
                e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

                //Graphics gr = e.Info.Graphics;
                //gr.PageUnit = GraphicsUnit.Pixel;
                //GridView view = sender as GridView;
                //SizeF size = gr.MeasureString(view.RowCount.ToString(), e.Appearance.Font);
                //view.IndicatorWidth = Convert.ToInt32(size.Width) + GridPainter.Indicator.ImageSize.Width + 5;
            }
        }
    }
}
